<?php
require('common.php');

$user = user();
$userno = $user . 'no';
$instance = sql_row1("SELECT * FROM Instances WHERE User='$user' AND Domain='$domain' AND Line_Number='$line'");
$bonus_field = $instance['Bonus_Field'];
$table = delete_from_table_table($bonus_field);

// an HTML form dropdown with all the available variables
// $name is the "name" tag in the <select> statement
function variables_dropdown($name)
{
	global $user, $domain, $bonus_field;
	$sql = "SELECT * FROM Variables WHERE (ID > -1 AND Domain='$domain' AND User='$user') AND Name IS NOT NULL ORDER BY ID DESC";
	//die($sql);
	$result = sql_exec($sql);
	if (mysql_num_rows($result) > 0)
	{
		$dropdown = "<select name=\"$name\">\n";
		
		// retrieve the previously selected variable for this field
		$selected_value = delete_from_table_default($bonus_field);
		//die($selected_value);
		$selected_value_id = substr($selected_value, strlen('$var'));
		//die("$selected_value_id");

		while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			$selected_toggle = ($selected_value_id == $row['ID']) ? "selected " : "";
			$dropdown .= '<option ' . $selected_toggle . 'value="' . $row['ID'] . '">' . $row['Name'] . "</option>\n";
		}
		$dropdown .= "</select>\n";
		return $dropdown;
	} else {
		die("make a variable.");
	}
}

// list all of the fields in this table as an ordered list
function all_fields($table, $bonus_field)
{
	$result = sql_exec("SHOW COLUMNS FROM $table");
	$default = delete_from_table_default_column($bonus_field);
	
	if (mysql_num_rows($result) > 0)
	{
		$dropdown = "<select name=\"column\">\n";
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			$field_name = $row['Field'];
			$selected_toggle = ($default == $field_name) ? " selected " : "";
			$dropdown .= '<option' . $selected_toggle . ">$field_name</option>\n";
		}
		$dropdown .= "</select>\n";
		return $dropdown;
	} else {
		die("this table is empty");
	}
}



// if the form was submitted, store the values
if (!empty($G['save']))
{
	$value = $G['value'];
	$column = $G['column'];

	$sql = "DELETE FROM $table WHERE $column='" . '$var' . "$value'";
	$sql = mysql_real_escape_string($sql);

	// store it in the instance
	sql_exec("UPDATE Instances SET Bonus_Field='$sql' WHERE User='$user' AND Domain='$domain' AND Line_Number='$line'");
	header("Location: index.php?domain=$domain");
}

html_header("edit table");
?>

<body>

<div id="container">

<?php header_div(); ?>

<div id="body">

<h3>Store in Table</h3>

<form action="delete_from_table.php" method="get">
<p>Delete the row with <?php echo all_fields($table, $bonus_field); ?> equal to: <?php echo variables_dropdown('value'); ?></p>
<input type="hidden" name="table" value="<?php echo $table ?>" />
<input type="hidden" name="domain" value="<?php echo $domain; ?>" />
<input type="hidden" name="line" value="<?php echo $line; ?>" />
<p><input type="submit" name="save" value="Save" /></p>
</form>

<p>
<form action="delete_instance.php" method="get">
<input type="hidden" name="domain" value="<?php echo $domain; ?>" />
<input type="hidden" name="line" value="<?php echo $line; ?>" />
<input type="submit" value="Delete This Action">
</form>
</p>

</div>

<?php footer(); ?>

</div>
</body>
</html>
